package cn.iocoder.yudao.module.promotion.activity.dal.mysql;

import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.promotion.activity.dal.dataobject.ActivityBaseEntity;
import cn.iocoder.yudao.module.promotion.activity.vo.ActivityQueryReqDTO;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 营销活动 Mapper
 *
 * @author 山野羡民
 */
//@org.apache.ibatis.annotations.Mapper;
public interface ActivityMapper<DO extends ActivityBaseEntity> extends CoreMapper<DO> {

    default PageResult<DO> selectPage(Integer current, Integer size, ActivityQueryReqDTO reqVO) {
        CoreQueryWrapper<DO> wrapper = new CoreQueryWrapper<>();
        wrapper.likeIfPresent("name", reqVO.getName());
        wrapper.eqIfPresent("status", reqVO.getStatus());
        wrapper.orderByDesc("create_time");
        return selectPage(current, size, wrapper);
    }

    default List<DO> selectListBySpuIdAndStatus(Long spuId, Integer status) {
        CoreQueryWrapper<DO> wrapper = new CoreQueryWrapper<>();
        wrapper.eq("spu_id", spuId);
        wrapper.eq("status", status);
        return selectList(wrapper);
    }

    default DO selectBySpuIdAndStatusAndNow(Long spuId, Integer status) {
        // 开始时间 < now < 结束时间，也就是说获取指定时间段的活动
        LocalDateTime now = LocalDateTime.now();
        CoreQueryWrapper<DO> wrapper = new CoreQueryWrapper<>();
        wrapper.eq("spu_id", spuId);
        wrapper.eq("status", status);
        wrapper.lt("start_time", now);
        wrapper.gt("end_time", now);
        wrapper.limitN(1);
        return selectOne(wrapper);
    }

}
